#include "ConcaveCooker.h"
#include "CookingStream.h"
#include <NxCooking.h>
#include <NxPhysics.h>

ConcaveCooker::ConcaveCooker(tstring inputFile, tstring outputFile):Cooker(inputFile,outputFile)
{
}

ConcaveCooker::~ConcaveCooker(void)
{
}

bool ConcaveCooker::Cook()
{
	NxCookingInterface *pCooking = NxGetCookingLib(NX_PHYSICS_SDK_VERSION);
	pCooking->NxInitCooking();

	NxActorDesc ActorDesc;

	MIT it;
	tstring of = this->GetOutputFile();
	string ofs = string(of.begin(),of.end());
	UserStream output(ofs.c_str(),false);
	MIT end = this->InputMeshEnd();
	bool success = true;
	for (it = this->InputMeshBegin(); it != end; ++it)
	{
		InputMesh* im = *it;
		UINT TriangleStrideBytes = 3*sizeof(int);

		// Build the triangle mesh.
		NxTriangleMeshDesc meshDesc;
		meshDesc.numVertices				= im->GetNumVertices();
		meshDesc.numTriangles				= im->GetNumFaces();
		meshDesc.pointStrideBytes			= im->GetNumBytesPerVertex();
		meshDesc.triangleStrideBytes		= TriangleStrideBytes;
		meshDesc.points						= im->GetVertexPointer();
		meshDesc.triangles					= im->GetTrianglesPointer();
		meshDesc.flags						= NX_MF_FLIPNORMALS ;

		bool imSuccess = pCooking->NxCookTriangleMesh(meshDesc,output);
		success = success && imSuccess;
	}
	pCooking->NxCloseCooking();
	fclose(output.fp);
	return success;
}